﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp_Autofac.Bll;
using WpfApp_Autofac.ViewModels;

namespace WpfApp_Autofac.Base;

public class AutofacViewModelLocator
{
    Autofac.IContainer container;

    public AutofacViewModelLocator()
    {
        ContainerBuilder containerBuilder = new ContainerBuilder();

        containerBuilder.RegisterType<LogInterceptor>();
        containerBuilder.RegisterType<MonitorInterceptor>();

        //通过虚方法实现Aop
        containerBuilder
            .RegisterType<MainWindowViewModel>()
            .PropertiesAutowired()
            .InterceptedBy(typeof(MonitorInterceptor))
            .InterceptedBy(typeof(LogInterceptor))
            .EnableClassInterceptors();

        //通过接口方法实现Aop
        containerBuilder
            .RegisterType<Auth>()
            .As<IAuth>()
            .InterceptedBy(typeof(MonitorInterceptor))
            .EnableInterfaceInterceptors();



        container = containerBuilder.Build();
    }


    public MainWindowViewModel MainWindowViewModel { get => container.Resolve<MainWindowViewModel>(); }
}
